package com.hlx.controller.user;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.hlx.entity.admin.TrainNumber;
import com.hlx.entity.admin.TrainNumberSeat;
import com.hlx.entity.user.TrainNumberStation;
import com.hlx.service.admin.PriceService;
import com.hlx.service.admin.StationService;
import com.hlx.service.admin.TrainNumberSeatService;
import com.hlx.service.admin.TrainNumberService;
import com.hlx.service.user.TrainNumberStationService;
import com.hlx.utils.Result;
import com.hlx.vo.TicketVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/ticketVo")
public class TicketVoController {
    @Autowired
    private StationService stationService;
    @Autowired
    private TrainNumberStationService trainNumberStationService;
    @Autowired
    private TrainNumberService trainNumberService;
    @Autowired
    private PriceService priceService;
    @Autowired
    private TrainNumberSeatService trainNumberSeatService;

    @GetMapping("/findTicket/{stationName}/{date}")

    public Result<List<TicketVo>> findTicket(@PathVariable("stationName") List<String> stationName, @PathVariable("date") Date date){
        //      判断是进京方向车辆，还是出京方向车辆
        Integer sequence = stationService.judgeBeijing(stationName);
        //      同时经过这俩个站的车
        List<String> trainNumber = trainNumberStationService.getTrainNumberByTwoName(stationName);
        //      筛出所有反方向车辆，并得到车次详情
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        List<TrainNumber> trainNumbers = trainNumberService.judgeTrain(trainNumber,sequence,localDate);
        Iterator<TrainNumber> itTrainNumbers = trainNumbers.iterator();
        //      计算俩地的距离
        Integer distance = stationService.cityDistance(stationName);
        //      计算票价(只保留1位)
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        Float businessPrice = Float.parseFloat(decimalFormat.format(distance * priceService.getById(1).getPrice()));
        Float firstPrice = Float.parseFloat(decimalFormat.format(distance * priceService.getById(2).getPrice()));
        Float secondPrice = Float.parseFloat(decimalFormat.format(distance * priceService.getById(3).getPrice()));
        //      根据车次号和车站名字，得到这俩个车站的信息
        List<String> trainNumberFin = trainNumbers.stream().map(item->item.getTrainNumberName()).collect(Collectors.toList());
        String start = stationName.get(0);
        Integer startId = stationService.getIdByStationName(start);
        String arrive = stationName.get(1);
        Integer arriveId = stationService.getIdByStationName(arrive);
        List<TicketVo> ticketBaseVos = new ArrayList<>();
        Iterator<String> itTrainNumberFin = trainNumberFin.iterator();

        while(itTrainNumberFin.hasNext()){
            TicketVo ticketVo = new TicketVo();
            String train = itTrainNumberFin.next();
            TrainNumber trainNumber1 = itTrainNumbers.next();
            //出发站和到达站的详情获取
            TrainNumberStation trainNumberStationStart = trainNumberStationService.getTrainNumberByStationNameAndTrainNumber(start,train);
            TrainNumberStation trainNumberStationEnd  = trainNumberStationService.getTrainNumberByStationNameAndTrainNumber(arrive,train);
            TrainNumberSeat trainNumberSeat = trainNumberSeatService.gerTrainNumberSeatByDateAndTrainNumberId(localDate,trainNumber1.getId());
            ticketVo.setTrainNumberId(trainNumber1.getId());
            ticketVo.setTrainNumber(train);
            ticketVo.setStartId(startId);
            ticketVo.setStartStation(start);
            ticketVo.setStartTime(trainNumberStationStart.getStartTime());
            ticketVo.setArriveId(arriveId);
            ticketVo.setArriveStation(arrive);
            ticketVo.setArriveTime(trainNumberStationEnd.getArriveTime());
            ticketVo.setTimeFin();
            ticketVo.setBusiness(trainNumberSeat.getBusiness());
            ticketVo.setBusinessPrice(businessPrice);
            ticketVo.setFirst(trainNumberSeat.getFirst());
            ticketVo.setFirstPrice(firstPrice);
            ticketVo.setSecond(trainNumberSeat.getSecond());
            ticketVo.setSecondPrice(secondPrice);
            ticketBaseVos.add(ticketVo);
        }
        ticketBaseVos.sort((item1, item2) -> item1.getStartTime().compareTo(item2.getStartTime()));
        if(ticketBaseVos.isEmpty()){
            return Result.error("此区间无车次");
        }
        return Result.success(ticketBaseVos);
    }
}
